<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Forum extends MY_Controller {
		
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /sys_admin/login');
			exit;
		}
		
		$this->tab = 'forum';
	}
	
	
	
	public function forum_upload_view(){
		
		$id = intval($_POST['id']);
		$vdata = array();
		if(isset($_POST['id']) && $id>0){
				$this->load->model('Forum_model');	
				$vdata['theme'] = $this->Forum_model->get_theme_obj($id);
		}
		$response_html = $this->load->view('/sys_admin/forum/theme_upload',$vdata,true);
		echo $response_html;
	}
	
	function forum_save(){
		
		$this->load->model('Forum_model');	
		
		$parms = array(	'name'=>$this->input->post("name"),'description'=>trim($this->input->post("description")));

		$id = $this->input->post("id");
		
		if(isset($_FILES['thumbnail']) && $_FILES['thumbnail']['name'] != ''){
			$thumbnail = $this->wrap_image(FORUM_IMG);
			$parms['thumbnail'] = $thumbnail;
		}

		
		if(isset($_POST['act']) && $_POST['act'] == 'delete' && $id > 0){
			$delparms['is_del'] = 1;
			$res = $this->Forum_model->update_theme($id,$delparms);
		}else if($id > 0){
			$res = $this->Forum_model->update_theme($id,$parms);
		}else{
			$res = $this->Forum_model->add_theme($parms);
		}
		
		if($res>0){
			$ret = array('msg'=>'操作成功');
		}else{
			$ret = array('msg'=>'操作失败');
		}
	
		echo json_encode($ret);
		exit; 
	}
	
	
	private function common_data_list($sub,$func){
		//接收参数
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$data = array();
		
		$data['page_size']  = sys_admin_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$data['where']['folder'] = 1;
		
		
		$this->load->model('Forum_model');
		
		$vdata = $this->Forum_model->$func($data);
		
		
		$vdata['forum_theme'] = $this->Forum_model->get_tname_list();
		$vdata['forum_section'] = $this->Forum_model->get_sname_list();
		$vdata['forum_status'] = $this->Forum_model->arrays('forum_status');
		$vdata['forum_shield'] = $this->Forum_model->arrays('forum_shield');
		
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/forum/'.$func.'/?';
		$config['total_rows'] = $vdata['forum_count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
	
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = $sub;
		
		$this->load->view('/sys_admin/forum/'.$sub, $vdata);
		
	}
	
	
	/**
	 * 
	 * 帖子列表
	 */
	function invitation_list(){
		$this->common_data_list('invitation', 'invitation_list');
	}
	
	
	private function common_post_view($id = 0){
		$this->load->model('Forum_model');
		$vdata['forum_theme'] = $this->Forum_model->get_tname_list();
		//$vdata['forum_section'] = $this->Forum_model->get_sname_list();
		$vdata['forum_status'] = $this->Forum_model->arrays('forum_status');
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'invitation';
		
		if($id > 0){
			$vdata['forum'] = $this->Forum_model->get_forum_post_obj($id);	
		}
		
		return $vdata;
		
	}
	
	/**
	 * 
	 * 添加帖子视图
	 */
	function add_view(){
		$vdata = $this->common_post_view();
		$this->load->view('/sys_admin/forum/invitation_add', $vdata);
	}
	
	/**
	 * 
	 * 编辑帖子视图
	 */
	function edit_view($id){
		$vdata = $this->common_post_view($id);		
		$this->load->view('/sys_admin/forum/invitation_edit', $vdata);
	}
	
	
	/**
	 * 
	 * 帖子视图详情
	 */
	function view($id){
		$vdata = $this->common_post_view($id);		
		
		//查询此帖的评论
	
		$this->load->model('Forum_model');
		
		$cdata['where']['pid'] = $id;
		
		$comment_count = $this->Forum_model->comment_list($cdata);
		//分页
		$num = 5;
		$post_data = $this->input->get();
		$page_obj 				= new stdClass();
		$page_obj->num_results	= $comment_count['forum_count'];
		$page_obj->num_pages		= ceil($page_obj->num_results / $num);
		$page_obj->pg			= isset($post_data['pg']) ? intval($post_data['pg']) : 1;
		$page_obj->pg			= min($page_obj->pg, $page_obj->num_pages);
		$page_obj->pg			= max($page_obj->pg, 1);
		$start_from			= ($page_obj->pg - 1) * $num;
		$page_obj->paging_url = "javascript:load_forum('{$id}', {p})";
		
		$pre_obj			= new stdClass();
				
		//加载列表
		$response_html='';
		
		$cdata['page_size']  = $num;
		$cdata['page_start'] = $start_from;
		
		$comment_list = $this->Forum_model->comment_list($cdata);
		foreach($comment_list['forum_list'] as $tmp){
			$pre_obj->p = $tmp;
			$data['D'] = $pre_obj;
			$response_html .= $this->load->view('/sys_admin/forum/forum_comment.php', $data,true);
		}
		
		$page_data['D'] 		= $page_obj;
    	$response_html .= $this->load->view('/sys_admin/forum/paging.php',$page_data,true);
		
		$from   = trim($this->input->get('from'));
		
		if( isset($from) && $from == 'ajax' )
    	{
    		echo $response_html;
    		exit;
    	}
    	
		$vdata['response_html'] = $response_html;	
							
		$this->load->view('/sys_admin/forum/invitation_view', $vdata);
	}
	
	private function common_post_data(){
		
		$data = array(
				'tid'=>intval($this->input->post('tid')),
				'subject'=>trim($this->input->post('subject')),
				'content'=>trim($this->input->post('content')),
				'tags'=>trim($this->input->post('tags')),
				'folder'=>1, 
			);
		  		
		if($data['tid'] == '' || $data['tid'] == 0){
			$ret['msg'] = '主题不能为空';
		}elseif($data['subject'] == ''){
			$ret['msg'] = '标题不能为空';
		}elseif($data['content'] == ''){
			$ret['msg'] = '帖子内容不能为空';	
		}
		
		if($ret['msg']){
			echo json_encode($ret);exit;
		}		
  		
		$author = trim($this->input->post('author'));
		$data['author']=$author;//给后台随意输入作者名而没加控制
		/* if($author){
			$this->load->model('User_model');
			$userInfo = $this->User_model->get_one(array('username'=>$author));
			if($userInfo){
				$data['author'] = $author;
				$data['authorid'] = $userInfo['user_id'];
			}else{
				$ret['msg'] = '发帖人不存在';	
				echo json_encode($ret);exit;
			}
		} */
		
  		return $data;
	}
	
	function invitation_save(){
		
		$data = $this->common_post_data();
		$act = trim($this->input->post('act'));
		
		$this->load->model('Forum_model');
		
		$id 		= intval($this->input->post('id'));
		
		if($act == 'add'){
			$data['add_time'] = time();
			$forum_id = $this->Forum_model->add_forum_post($data);
		}else{
			$data['fstatus'] = intval($this->input->post('fstatus'));
			$forum_id = $this->Forum_model->update_forum_post($id, $data);
			
		/*if($forum_id && $data['fstatus']==1){
				$cdata['pid'] = $id;
				$cdata['author'] = $data['author'];
				$cdata['authorid'] = $data['authorid'];
				$cdata['comment'] = $data['content'];
				$cdata['add_time'] =time();
				$cdata['is_floor'] = 1;
				$this->Forum_model->add_comment($cdata);
				$this->Forum_model->set_recovery_post($id);
			}*/
		}
		
		$ret['msg'] = $forum_id;
		echo json_encode($ret);
   		exit;
	}
	
	
	
	/**
	 * 
	 * 版块列表
	 */
	function section_list(){
		$this->common_data_list('section', 'section_list');
	}
	
	
	/**
	 * 
	 * 主题列表
	 */
	function theme_list(){
		$this->common_data_list('theme', 'theme_list');
	}
	
	
	
	function forum_coment_save(){
		$id 		= intval($this->input->post('id'));
		$data['is_del'] = 1;
		$this->load->model('Forum_model');
		$forum_id = $this->Forum_model->edit_comment($id, $data);
		$ret['msg'] = $forum_id;
		echo json_encode($ret);
   		exit;
	}
	
	
	function forum_turn(){
		
		$this->load->model('Forum_model');
		
		$act = $this->input->post('act');
		
		$id 		= intval($this->input->post('id'));
		
		$post_obj = $this->Forum_model->get_forum_post_obj($id);
		if($act && $act=="first"){
			if($post_obj['first'] == 1){$data['first'] = 0;}else{$data['first'] = 1;}
			
		}else if($act=="shield"){
			if($post_obj['is_shield'] == 1){$data['is_shield'] = 0;}else{$data['is_shield'] = 1;}
		}
		
		$forum_id = $this->Forum_model->update_forum_post($id,$data);

		$ret['msg'] = $forum_id;
		echo json_encode($ret);
   		exit;
	}
	
	
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Forum extends MY_Controller {
    	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /sys_admin/login');
			exit;
		}
		
		$this->tab = 'forum';
	}
	
	
	
	public function forum_upload_view(){
		
		$id = intval($_POST['id']);
		$vdata = array();
		if(isset($_POST['id']) && $id>0){
				$this->load->model('Forum_model');	
				$vdata['theme'] = $this->Forum_model->get_theme_obj($id);
		}
		$response_html = $this->load->view('/sys_admin/forum/theme_upload',$vdata,true);
		echo $response_html;
	}
	
	function forum_save(){
		
		$this->load->model('Forum_model');	
		
		$parms = array(	'name'=>$this->input->post("name"),'description'=>trim($this->input->post("description")));

		$id = $this->input->post("id");
		
		if(isset($_FILES['thumbnail']) && $_FILES['thumbnail']['name'] != ''){
			$thumbnail = $this->wrap_image(FORUM_IMG);
			$parms['thumbnail'] = $thumbnail;
		}

		
		if(isset($_POST['act']) && $_POST['act'] == 'delete' && $id > 0){
			$delparms['is_del'] = 1;
			$res = $this->Forum_model->update_theme($id,$delparms);
		}else if($id > 0){
			$res = $this->Forum_model->update_theme($id,$parms);
		}else{
			$res = $this->Forum_model->add_theme($parms);
		}
		
		if($res>0){
			$ret = array('msg'=>'操作成功');
		}else{
			$ret = array('msg'=>'操作失败');
		}
	
		echo json_encode($ret);
		exit; 
	}
	
	
	private function common_data_list($sub,$func){
		//接收参数
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$data = array();
		
		$data['page_size']  = sys_admin_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$data['where']['folder'] = 1;
		
		
		$this->load->model('Forum_model');
		
		$vdata = $this->Forum_model->$func($data);
		
		
		$vdata['forum_theme'] = $this->Forum_model->get_tname_list();
		$vdata['forum_section'] = $this->Forum_model->get_sname_list();
		$vdata['forum_status'] = $this->Forum_model->arrays('forum_status');
		$vdata['forum_shield'] = $this->Forum_model->arrays('forum_shield');
		
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/forum/'.$func.'/?';
		$config['total_rows'] = $vdata['forum_count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
	
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = $sub;
		
		$this->load->view('/sys_admin/forum/'.$sub, $vdata);
		
	}
	
	
	/**
	 * 
	 * 帖子列表
	 */
	function invitation_list(){
		$this->common_data_list('invitation', 'invitation_list');
	}
	
	
	private function common_post_view($id = 0){
		$this->load->model('Forum_model');
		$vdata['forum_theme'] = $this->Forum_model->get_tname_list();
		//$vdata['forum_section'] = $this->Forum_model->get_sname_list();
		$vdata['forum_status'] = $this->Forum_model->arrays('forum_status');
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'invitation';
		
		if($id > 0){
			$vdata['forum'] = $this->Forum_model->get_forum_post_obj($id);	
		}
		
		return $vdata;
		
	}
	
	/**
	 * 
	 * 添加帖子视图
	 */
	function add_view(){
		$vdata = $this->common_post_view();
		$this->load->view('/sys_admin/forum/invitation_add', $vdata);
	}
	
	/**
	 * 
	 * 编辑帖子视图
	 */
	function edit_view($id){
		$vdata = $this->common_post_view($id);		
		$this->load->view('/sys_admin/forum/invitation_edit', $vdata);
	}
	
	
	/**
	 * 
	 * 帖子视图详情
	 */
	function view($id){
		$vdata = $this->common_post_view($id);		
		
		//查询此帖的评论
	
		$this->load->model('Forum_model');
		
		$cdata['where']['pid'] = $id;
		
		$comment_count = $this->Forum_model->comment_list($cdata);
		//分页
		$num = 5;
		$post_data = $this->input->get();
		$page_obj 				= new stdClass();
		$page_obj->num_results	= $comment_count['forum_count'];
		$page_obj->num_pages		= ceil($page_obj->num_results / $num);
		$page_obj->pg			= isset($post_data['pg']) ? intval($post_data['pg']) : 1;
		$page_obj->pg			= min($page_obj->pg, $page_obj->num_pages);
		$page_obj->pg			= max($page_obj->pg, 1);
		$start_from			= ($page_obj->pg - 1) * $num;
		$page_obj->paging_url = "javascript:load_forum('{$id}', {p})";
		
		$pre_obj			= new stdClass();
				
		//加载列表
		$response_html='';
		
		$cdata['page_size']  = $num;
		$cdata['page_start'] = $start_from;
		
		$comment_list = $this->Forum_model->comment_list($cdata);
		foreach($comment_list['forum_list'] as $tmp){
			$pre_obj->p = $tmp;
			$data['D'] = $pre_obj;
			$response_html .= $this->load->view('/sys_admin/forum/forum_comment.php', $data,true);
		}
		
		$page_data['D'] 		= $page_obj;
    	$response_html .= $this->load->view('/sys_admin/forum/paging.php',$page_data,true);
		
		$from   = trim($this->input->get('from'));
		
		if( isset($from) && $from == 'ajax' )
    	{
    		echo $response_html;
    		exit;
    	}
    	
		$vdata['response_html'] = $response_html;	
							
		$this->load->view('/sys_admin/forum/invitation_view', $vdata);
	}
	
	private function common_post_data(){
		
		$data = array(
				'tid'=>intval($this->input->post('tid')),
				'subject'=>trim($this->input->post('subject')),
				'content'=>trim($this->input->post('content')),
				'tags'=>trim($this->input->post('tags')),
				'folder'=>1, 
			);
		  		
		if($data['tid'] == '' || $data['tid'] == 0){
			$ret['msg'] = '主题不能为空';
		}elseif($data['subject'] == ''){
			$ret['msg'] = '标题不能为空';
		}elseif($data['content'] == ''){
			$ret['msg'] = '帖子内容不能为空';	
		}
		
		if($ret['msg']){
			echo json_encode($ret);exit;
		}		
  		
		$author = trim($this->input->post('author'));
		$data['author']=$author;//给后台随意输入作者名而没加控制
		/* if($author){
			$this->load->model('User_model');
			$userInfo = $this->User_model->get_one(array('username'=>$author));
			if($userInfo){
				$data['author'] = $author;
				$data['authorid'] = $userInfo['user_id'];
			}else{
				$ret['msg'] = '发帖人不存在';	
				echo json_encode($ret);exit;
			}
		} */
		
  		return $data;
	}
	
	function invitation_save(){
		
		$data = $this->common_post_data();
		$act = trim($this->input->post('act'));
		
		$this->load->model('Forum_model');
		
		$id 		= intval($this->input->post('id'));
		
		if($act == 'add'){
			$data['add_time'] = time();
			$forum_id = $this->Forum_model->add_forum_post($data);
		}else{
			$data['fstatus'] = intval($this->input->post('fstatus'));
			$forum_id = $this->Forum_model->update_forum_post($id, $data);
			
		/*if($forum_id && $data['fstatus']==1){
				$cdata['pid'] = $id;
				$cdata['author'] = $data['author'];
				$cdata['authorid'] = $data['authorid'];
				$cdata['comment'] = $data['content'];
				$cdata['add_time'] =time();
				$cdata['is_floor'] = 1;
				$this->Forum_model->add_comment($cdata);
				$this->Forum_model->set_recovery_post($id);
			}*/
		}
		
		$ret['msg'] = $forum_id;
		echo json_encode($ret);
   		exit;
	}
	
	
	
	/**
	 * 
	 * 版块列表
	 */
	function section_list(){
		$this->common_data_list('section', 'section_list');
	}
	
	
	/**
	 * 
	 * 主题列表
	 */
	function theme_list(){
		$this->common_data_list('theme', 'theme_list');
	}
	
	
	
	function forum_coment_save(){
		$id 		= intval($this->input->post('id'));
		$data['is_del'] = 1;
		$this->load->model('Forum_model');
		$forum_id = $this->Forum_model->edit_comment($id, $data);
		$ret['msg'] = $forum_id;
		echo json_encode($ret);
   		exit;
	}
	
	
	function forum_turn(){
		
		$this->load->model('Forum_model');
		
		$act = $this->input->post('act');
		
		$id 		= intval($this->input->post('id'));
		
		$post_obj = $this->Forum_model->get_forum_post_obj($id);
		if($act && $act=="first"){
			if($post_obj['first'] == 1){$data['first'] = 0;}else{$data['first'] = 1;}
			
		}else if($act=="shield"){
			if($post_obj['is_shield'] == 1){$data['is_shield'] = 0;}else{$data['is_shield'] = 1;}
		}
		
		$forum_id = $this->Forum_model->update_forum_post($id,$data);

		$ret['msg'] = $forum_id;
		echo json_encode($ret);
   		exit;
	}
	
	
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
}